#import "Switcher.h"

void dolog(char* formatstring,...)
{
    va_list arglist;
    va_start(arglist, formatstring);
    vfprintf(stderr, formatstring, arglist);
    fprintf(stderr, "\n");
    fflush(stderr);
    va_end(arglist);
}

@implementation Notes2
- (void) applicationWillTerminate {
}

- (void) checkLicense {
    NSString *month = [[NSDate date] 
		descriptionWithCalendarFormat:@"%m" 
		timeZone:nil 
		locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
	if ([month compare:@"05"] != NSOrderedSame) {
		dolog("License expired, exiting");
		[self terminate];
	}
}

- (void) applicationDidFinishLaunching: (id) unused {
    freopen("/tmp/switcher.log", "a", stderr);

    dolog("application startup");
	
//	[self checkLicense];
	
    window = [ [ UIWindow alloc ] initWithContentRect:
        [ UIHardware fullScreenApplicationContentRect ]
    ];
	
    CGRect rect = [ UIHardware fullScreenApplicationContentRect ];
    rect.origin.x = rect.origin.y = 0.0f;

    mainView = [[MainView alloc] initWithFrame:rect controller:self];
    [ window setContentView: mainView ];
    [ window orderFront: self ];
    [ window makeKey: self ];
    [ window _setHidden: NO ];
    dolog("window created");
}



- (NSDictionary *)getPreferences {   
	if(prefs == nil) {
		dolog("loading prefs...");
		NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Connection.plist"];
//		dolog("path %s",[path UTF8String]);
		prefs = [NSDictionary dictionaryWithContentsOfFile:path];
//		prefs = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/com.danila.switcher" stringByExpandingTildeInPath]];
		if(prefs == nil) {
			dolog("can't load prefs, creating new");
			prefs = [[NSDictionary alloc] init];
		}
		dolog("loading finished: %s",[[prefs description] UTF8String]);
		prefs = [prefs retain];
	}
	return prefs;
}

- (void)savePreferences:(NSDictionary *) _prefs {
	[prefs release];
	prefs = _prefs;
	dolog("going to save");
    BOOL result = [prefs writeToFile:[@"~/Library/Preferences/com.danila.switcher" stringByExpandingTildeInPath] atomically:NO];
	prefs = [prefs retain];
}
@end

